{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "82bc2424",
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "该数据集提供了全球化石二氧化碳排放量的国家级调查，\n",
    "包括总排放量、煤炭、石油、天然气、水泥、燃除和其他\n",
    "来源的排放量以及人均排放量。对于希望按国家量化全球\n",
    "二氧化碳排放水平并了解这些排放来源的研究人员来说，\n",
    "这个数据集可能是一个宝贵的资源。该数据集包括了从\n",
    "1750年至2021年的各个国家的数据。\n",
    "\"\"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fab15a63",
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "目标1：\n",
    "使用pandas读取数据并且进行数据清洗和预处理。\n",
    "  1.首先国家的ISO 3166-1 alpha-3代码对本文的分\n",
    "    析没有多大用处，就过滤掉该字段。\n",
    "  2.由于该数据是从1750年开始收集的，由于种种原因，\n",
    "    总有一些国家的某些年数据是丢失的。因此，过滤掉\n",
    "    某个国家某年数据全为空的行。\n",
    "  3.该数据集中存在“global 和 International Transport”。\n",
    "    这两个字段表示全球和国际排放量。特别地，这两个数据\n",
    "    不代表国家，因此过滤掉这两个数据。\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "39a9cdc4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6c8e9904",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_csv('C:/Users/Administrator/Desktop/demo/GCB2022v27_MtCO2_flat.csv') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7dcdb679",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = data.fillna(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4e5e94bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "targetFile = 'C:/Users/Administrator/Desktop/demo/result.csv'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "334fc6d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(targetFile,'w+',encoding='utf-8') as f:\n",
    "    for line in data.values:\n",
    "        # 过滤掉 “global 和 International Transport”\n",
    "        if line[0] == \"Global\" or line[0] == \"International Transport\":  \n",
    "            continue\n",
    "        # 忽略空数据\n",
    "        if line[3] == 0.0 :\n",
    "            continue\n",
    "        f.write(str(line[0])+'\\t'+str(line[2])+'\\t'+str(line[3])+'\\t'+str(line[4])+'\\t'+str(line[5])+'\\t'+str(line[6])+'\\t'+str(line[7])+'\\t'+str(line[8])+'\\t'+str(line[9])+'\\t'+str(line[10])+'\\n') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f1ce0a0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
